package com.mazaiting.instructions.constants;

import com.mazaiting.instructions.base.BytecodeReader;
import com.mazaiting.instructions.base.Instruction;
import com.mazaiting.rtda.MtFrame;

/**
 * 从操作数中获取一个整数
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class IPushInstruction {
    /**
     * bipush 指令从操作数中获取一个byte型整数，扩展成int型，然后推入栈顶
     */
    public static class BiPush implements Instruction {
        /**
         * u1 值
         */
        private byte value;

        @Override
        public void fetchOperands(BytecodeReader reader) {
            this.value = reader.readU1();
        }

        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(value);
        }

        @Override
        public String toString() {
            return "bipush " + value;
        }
    }

    /**
     * sipush指令从操作数中获取一个short型整数，扩展成int型，然后推入栈顶
     */
    public static class SiPush implements Instruction {
        /**
         * u2 值
         */
        private short value;

        @Override
        public void fetchOperands(BytecodeReader reader) {
            this.value = reader.readU2();
        }

        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(value);
        }

        @Override
        public String toString() {
            return "sipush " + value;
        }
    }
}
